package gui.specialGUIs;

import definitions.Commands;
import entities.Member;
import entities.Participant;
import gui.designComponents.TMTCheckBox;
import gui.designComponents.TMTComboBox;
import gui.designComponents.TMTDefaultFormBuilder;
import gui.designComponents.TMTNormalButton;
import gui.designComponents.TMTPanel;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.ResourceBundle;

import app.Controller;
import app.Model;
import bundles.BundleLoader;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

/**
 * Shows Participants of a Event and allows to edit the rating
 * 
 * @author MK
 * 
 */
public class ParticipantsPanel extends TMTPanel {

	private Model model;
	private Controller controller;

	private DefaultFormBuilder builder;
	private ArrayList<TMTCheckBox> participatingCheckBoxes;
	private ParticipantsButtonPanel buttonPanel;
	private ArrayList<TMTComboBox> ratingComboBoxes;

	public ParticipantsPanel(Model model, Controller controller) {

		this.model = model;
		this.controller = controller;

		setLayout(new BorderLayout());

		FormLayout layout = new FormLayout(
				"right:max(40dlu;p), 4dlu, 60dlu, 5dlu, "
						+ "right:max(40dlu;p), 4dlu, 50dlu, 5dlu ", 
						"");

		builder = TMTDefaultFormBuilder.getDFB(layout);

		add(builder.getPanel(), BorderLayout.CENTER);

		this.buttonPanel = new ParticipantsButtonPanel(controller);
		add(buttonPanel, BorderLayout.SOUTH);
	}

	public void update() {

		remove(builder.getPanel());
		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		ArrayList<Member> completeMemberList = model
				.getMemberEntriesAsArrayList();
		ArrayList<Participant> participantsOfEvent = model.getSelectedEvent()
				.getParticipants();

		ArrayList<Member> memberOfEvent = new ArrayList<Member>();

		int[] ratings = new int[participantsOfEvent.size()];
		int ratingCounter = 0;

		for (Participant p : participantsOfEvent) {
			if (p!=null){
			memberOfEvent.add(p.getMember());
			ratings[ratingCounter++] = p.getRating();
			}
		}

		participatingCheckBoxes = new ArrayList<TMTCheckBox>();
		ratingComboBoxes = new ArrayList<TMTComboBox>();

		FormLayout layout = new FormLayout(
				"right:max(40dlu;p), 4dlu, 60dlu, 5dlu, "
						+ "right:max(40dlu;p), 4dlu, 50dlu, 5dlu ", "");

		builder = TMTDefaultFormBuilder.getDFB(layout);
		builder.appendSeparator(resBundle.getString("participants"));

		ratingCounter = 0;
		for (Member member : completeMemberList) {

			TMTCheckBox checkBox = new TMTCheckBox(
					(member.getNameFirst() + " " + member.getNameLast()));
			checkBox.setActionCommand(Commands.PARTICIPANTS_UPDATE_LIVE);
			checkBox.addActionListener(controller);

			TMTComboBox ratingBox = new TMTComboBox(model.getRatingValues());

			participatingCheckBoxes.add(checkBox);
			ratingComboBoxes.add(ratingBox);

			if (memberOfEvent.contains(member)) {
				checkBox.setSelected(true);
				ratingBox.setSelectedIndex(ratings[ratingCounter++]);
			}
			builder.append("", checkBox);
			if (memberOfEvent.contains(member)) {
				builder.append(resBundle.getString("rating"), ratingBox);
			}
			builder.nextLine();
		}
		add(builder.getPanel());
	}

	public ArrayList<TMTCheckBox> getCheckBoxes() {
		return participatingCheckBoxes;
	}

	public ArrayList<TMTComboBox> getRatingComboBoxes() {
		return ratingComboBoxes;
	}
}

class ParticipantsButtonPanel extends TMTPanel {

	public ParticipantsButtonPanel(Controller controller) {

		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		TMTNormalButton submitButton = new TMTNormalButton(resBundle
				.getString("submit"));
		submitButton.setActionCommand(Commands.PARTICIPANTS_UPDATE);
		submitButton.addActionListener(controller);
		add(submitButton, BorderLayout.SOUTH);

		TMTNormalButton backButton = new TMTNormalButton(resBundle
				.getString("back"));
		backButton.setActionCommand(Commands.GO_BACK);
		backButton.addActionListener(controller);
		add(backButton, BorderLayout.SOUTH);
	}
}